﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YJKRoomInspection.Utiltiy;
using YArchitech.LIB;
using YJKRevitBase.WinForm;

namespace YJKRoomInspection.ShowDataForm
{
	public partial class SelectFloorForm : System.Windows.Forms.Form
	{
		public bool m_IsReFindTag
		{
			get
			{
				return this.isReFindTag;
			}
			set
			{
				this.isReFindTag = value;
			}
		}

		public bool IsCommit
		{
			get
			{
				return this.m_isCommit;
			}
			set
			{
				this.m_isCommit = value;
			}
		}

		public List<int> SelectedViewIndexs()
		{
			List<int> list = new List<int>();
			for (int i = 0; i < this.chkList.Items.Count; i++)
			{
				if (this.chkList.GetItemChecked(i))
				{
					list.Add(i);
				}
			}
			return list;
		}

		public SelectFloorForm(List<ViewPlan> views, bool IsReFindTag, bool ifEvacuationDistance = false)
		{
			this.m_Views = views;
			List<string> list = new List<string>();
			this.m_IsReFindTag = IsReFindTag;
			this.InitializeComponent();
            SelectFloorForm._hWndSelectFloorForm = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			StaticRescource.m_hWndSelectFloorForm = SelectFloorForm._hWndSelectFloorForm;
			for (int i = 0; i < this.m_Views.Count; i++)
			{
				ViewPlan viewPlan = this.m_Views[i];
				list.Add(viewPlan.Name);
			}
			this.ReadLayerCheckd(ref list);
			for (int j = 0; j < this.m_Views.Count; j++)
			{
				ViewPlan view = this.m_Views[j];
				this.chkList.Items.Add(view.Name);
				if (list.Find((string name) => name == RevitVersionFuncs.GetViewName(view)) == null)
				{
					this.chkList.SetItemChecked(j, false);
				}
				else
				{
					this.chkList.SetItemChecked(j, true);
				}
			}
			if (ifEvacuationDistance)
			{
				this.ReCheckBtn_YJKRoomInspection.Visible = false;
				this.labe_EvacuationDistance.Visible = true;
			}
			this.ReCheckBtn_YJKRoomInspection.Checked = this.m_IsReFindTag;
		}

		private void SelectFloorForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			if (!string.IsNullOrEmpty(this.strFormCaption))
			{
				this.Text = this.strFormCaption;
			}
		}

		private void SelectAll_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.chkList.Items.Count; i++)
			{
				this.chkList.SetItemChecked(i, true);
			}
		}

		private void NoSelectAll_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.chkList.Items.Count; i++)
			{
				this.chkList.SetItemChecked(i, false);
			}
		}

		private void ReverseSelect_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.chkList.Items.Count; i++)
			{
				bool value = !this.chkList.GetItemChecked(i);
				this.chkList.SetItemChecked(i, value);
			}
		}

		private void OkBtn_Click(object sender, EventArgs e)
		{
			this.m_isCommit = true;
			if (this.ReCheckBtn_YJKRoomInspection.Checked)
			{
				this.isReFindTag = true;
			}
			else
			{
				this.isReFindTag = false;
			}
			this.GetFloorToFile();
			base.Close();
		}

		private void CancelBtn_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void GetFloorToFile()
		{
			FileStream fileStream = new FileStream(Product.UserDataLocation + "\\SetLayer.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			int x = base.Location.X;
			int y = base.Location.Y;
			streamWriter.WriteLine(x.ToString());
			streamWriter.WriteLine(y.ToString());
			int num = 0;
			for (int i = 0; i < this.chkList.Items.Count; i++)
			{
				bool itemChecked = this.chkList.GetItemChecked(i);
				string strLayerName = this.chkList.GetItemText(this.chkList.Items[i]);
				ViewPlan key = this.m_Views.Find((ViewPlan layer) => RevitVersionFuncs.GetViewName(layer) == strLayerName);
				if (itemChecked)
				{
					num = 1;
					streamWriter.WriteLine(strLayerName);
				}
				if (num == 1 && !this.m_LevelStatus.ContainsKey(key))
				{
					this.m_LevelStatus.Add(key, itemChecked);
				}
			}
			streamWriter.Close();
			fileStream.Close();
			ViewPlan viewPlan = null;
			foreach (KeyValuePair<ViewPlan, bool> keyValuePair in this.m_LevelStatus)
			{
				if (keyValuePair.Value)
				{
					viewPlan = keyValuePair.Key;
				}
			}
			List<ViewPlan> list = new List<ViewPlan>();
			foreach (KeyValuePair<ViewPlan, bool> keyValuePair2 in this.m_LevelStatus)
			{
				if (keyValuePair2.Key.GenLevel.Elevation > viewPlan.GenLevel.Elevation)
				{
					list.Add(keyValuePair2.Key);
				}
			}
			if (list != null)
			{
				foreach (ViewPlan key2 in list)
				{
					this.m_LevelStatus.Remove(key2);
				}
			}
		}

		private void ReadLayerCheckd(ref List<string> SelectName)
		{
			string path = Product.UserDataLocation + "\\SetLayer.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				return;
			}
			SelectName.Clear();
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			base.StartPosition = FormStartPosition.Manual;
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			for (;;)
			{
				string text = streamReader.ReadLine();
				if (text == null)
				{
					break;
				}
				SelectName.Add(text);
			}
			streamReader.Close();
			fileStream.Close();
		}

		private void SelectFloorForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		private void chkList_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		public string strFormCaption;

        public static WindowHandle _hWndSelectFloorForm;

		private List<ViewPlan> m_Views = new List<ViewPlan>();

		private List<Level> m_Levels = new List<Level>();

		private Dictionary<ViewPlan, bool> m_LevelStatus = new Dictionary<ViewPlan, bool>();

		private bool m_isCommit;

		private bool isReFindTag;
	}
}
